package com.sc.nft.hna.form;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.sc.nft.config.hna.HnaConfig;
import com.sc.nft.dto.HnaWithdrawDto;
import com.sc.nft.exception.GlobalRunTimeException;
import lombok.Data;

import java.util.Map;

@Data
public class T002TransForm extends BaseTransForm {

  public static final String[] encryptArr =
      new String[] {
        "tranAmount",
        "userId",
        "bindCardAgrNo",
        "notifyUrl",
        "paymentTerminalInfo",
        "deviceInfo",
        "serviceAmount",
        "feeAmtType",
        "businessType"
      };
  public static final String[] verifyArr =
      new String[] {
        "version",
        "tranCode",
        "merOrderId",
        "merId",
        "charset",
        "signType",
        "resultCode",
        "errorCode",
        "ncountOrderId"
      };
  public static final String[] notifyVerifyArr = verifyArr;
  public static final String[] submitArr =
      new String[] {
        "version",
        "tranCode",
        "merId",
        "merOrderId",
        "submitTime",
        "msgCiphertext",
        "signType",
        "signValue",
        "merAttach",
        "charset"
      };
  private static final long serialVersionUID = -5629269950395397553L;

  private String tranAmount; // 支付金额
  private String userId; // 提现用户编号
  private String bindCardAgrNo; // 绑卡协议号
  private String notifyUrl; // 异步通知地址
  private String paymentTerminalInfo; // 付款方终端信息
  private String deviceInfo; // 设备信息
  private String serviceAmount; // 服务费
  private String feeAmtType;
  private String businessType; // 业务类型
  private String userName; // 持卡人姓名
  private String cardNo; // 银行卡号

  public static final String MAIN_URL = "https://ncount.hnapay.com/api/t002.htm";
  public static final String TRAN_CODE = "T002";

  @Override
  public String getEncryptJsonStr() {
    return getJsonStr(this, encryptArr);
  }

  @Override
  public String getVerifyJsonStr() {
    return getJsonStr(this, verifyArr);
  }

  @Override
  public String getSubmitJsonStr() {
    return getJsonStr(this, submitArr);
  }

  public static Map<String, Object> generateParam(
      HnaConfig hnaConfig, HnaWithdrawDto dto, String customId) {
    T002TransForm transForm = new T002TransForm();
    transForm.initCommonParams(TRAN_CODE, hnaConfig.getMerId(), customId, "");
    if (!NumberUtil.isNumber(dto.getTranAmount())) {
      throw new GlobalRunTimeException("提现金额非法");
    }
    transForm.setTranAmount(dto.getTranAmount());
    transForm.setBindCardAgrNo(dto.getBindCardAgrNo());
    transForm.setNotifyUrl(dto.getNotifyUrl());
    transForm.setUserId(dto.getUserId());
    transForm.setServiceAmount(dto.getFeeAmount());
    transForm.setPaymentTerminalInfo(dto.getPaymentTerminalInfo());
    transForm.setDeviceInfo(dto.getDeviceInfo());
    return generateParam(transForm, hnaConfig);
  }
}
